{% extends '_base_list.html' %}

{% block title %}
    代码发布
{% endblock %}


{% block top_css %}
    {{ super() }}
    <!--bees-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bees.css') }}">

{% endblock %}


<!-- Content head -->
{% block head_title %}
    代码发布
{% endblock %}
{% block head_nav %}
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-home"></i></a></li>
        <li><a href="#">代码发布</a></li>
        <li class="active">工单审核</li>
    </ol>
{% endblock %}


<!-- Content body -->
{% block title_filter %}
    <div class="filter">
        <div class="row">
            <div class="col-xs-12 col-sm-2">
                <div class="form-group">
                    <input type="text" class="form-control" name="create_time" placeholder="请选择创建时间" autocomplete="off">
                </div>
            </div>
            <div class="col-xs-12 col-sm-2">
                <div class="form-group">
                    <select class="select2 form-control" name="project" data-placeholder="项目名称">
                    </select>
                </div>
            </div>
            <div class="col-xs-12 col-sm-3 pull-right">
                <div class="form-group">
                    <div class="input-group">
                        <input type="text" name="search" placeholder="请输入上线标题" class="form-control">
                        <span class="input-group-btn">
                            <button name="search" class="btn btn-mint" type="button">
                                <i class="fa fa-search"></i>
                            </button>
                        </span>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block body_operator %}
    <div class="operator mar-btm">
        <div class="row">
            <div class="col-xs-12 text-center">
                {% if current_user.has_perm('codes-audit-approval') %}
                    <button class="btn btn-mint btn-sm" id="approval"><i class=""></i> 批准上线</button>
                {% endif %}
                {% if current_user.has_perm('codes-audit-refuse') %}
                    <button class="btn btn-danger btn-sm" id="refuse"><i class=""></i> 拒绝上线</button>
                {% endif %}
            </div>
        </div>
    </div>
{% endblock %}

{% block body_table %}
   <table id="approvalTable" class="table table-auto">
   </table>
{% endblock %}


{% block buttom_js %}
    {{ super() }}
    <!--bees-->
    <script src="{{ url_for('static', filename='js/bees.js') }}"></script>

    <script type="text/javascript">
        $(function () {
            bees.initServerSideSelect2(
                $('select[name="project"]'),
                {url: '{{ url_for('codes.api-project-list') }}'}
            );

            bees.initDaterangepicker($('input[name="create_time"]'),{
                opens: "right",
                timePicker: false,
                timePickerSeconds: false,
                singleDatePicker: false,
                locale: {
                    format: 'YYYY-MM-DD'
                }
            });

            // 回填和初始化默认过滤参数
            var createTimeEle = $("input[name='create_time']");
            var projectEle = $("select[name='project']");
            var searchEle = $('input[name="search"]');

            var filterParams = JSON.parse(window.sessionStorage.getItem("filterParams"));
            if(filterParams){
                // 回填表单过滤条件
                createTimeEle.val(filterParams.create_time);
                bees.select2DataEcho(
                    projectEle,
                    {
                        url: '{{ url_for('codes.api-project-list') }}',
                        selected: [filterParams.role],
                        async: false    // 只能通过同步的方式
                    }
                );
                searchEle.val(filterParams.search);
                window.sessionStorage.removeItem("filterParams");
            }else {
                // 默认表单过滤条件
                filterParams = {
                    'create_time': createTimeEle.val(),
                    'project': projectEle.val(),
                    'search': searchEle.val()
                };
            }
            $.fn.beesTable.defaultFilterParams(filterParams);

            // 初始化表格
            var $tableEle = $("#approvalTable");
            var options = {
                columns: [
                    {title: '', field: 'selected', radio: true},
                    {title: '项目', field: 'project', sortable: false},
                    {title: '上线标题', field: 'title', sortable: false},
                    {title: '上线版本', field: 'revision', sortable: false},
                    {title: '上线状态', field: 'status', sortable: false,
                        formatter: function (value, row, index, field) {
                            // value: 字段值; row: 行记录数据; index: 行索引 ;field: 行字段
                            if(row.action == 1){
                                if(row.status === 0){
                                    return '<span class="label label-info">新建提交 - <b class="text-danger">待审核</b></span>'
                                }
                                else if(row.status === 1){
                                    return '<span class="label label-mint">审核通过 - <b class="text-danger">待上线</b></span>'
                                }
                                else if(row.status === 2){
                                    return '<span class="label label-main">审核拒绝</span>'
                                }
                                else if(row.status === 3){
                                    return '<span class="label label-warning">正在上线</span>'
                                }
                                else if(row.status === 4){
                                    return '<span class="label label-success">上线完成</span>'
                                }
                                else if(row.status === 5){
                                    return '<span class="label label-danger">上线失败</span>'
                                }
                                else if(row.status === 6){
                                    return '<span class="label label-success">上线成功 - <b class="text-danger">已回滚</b></span>'
                                }
                            }else if(row.action == 2){
                                if(row.status === 0){
                                    return '<span class="label label-info">新建提交 - <b class="text-danger">待回滚</b></span>'
                                }
{#                                else if(row.status === 1){#}
{#                                    return '<span class="label label-mint">审核通过</span>'#}
{#                                }#}
{#                                else if(row.status === 2){#}
{#                                    return '<span class="label label-main">审核拒绝</span>'#}
{#                                }#}
                                else if(row.status === 3){
                                    return '<span class="label label-warning">正在回滚</span>'
                                }
                                else if(row.status === 4){
                                    return '<span class="label label-purple">回滚完成</span>'
                                }
                                else if(row.status === 5){
                                    return '<span class="label label-danger">回滚失败</span>'
                                }
                            }
                        }
                    },
                    {title: '创建人', field: 'create_by'},
                    {title: '创建时间', field: 'create_time', sortable: true}
                ],
                url:'{{ url_for("codes.api-audit-list") }}',
                sortName: 'create_time',
                sortOrder: 'desc'
            };
            var table = $tableEle.beesTable(options);

            // 搜索
            bees.bindTableSearch({
                table: $tableEle,
                searches: [
                    {eventType:'change', eventSource:createTimeEle, data:function () {
                            return {'create_time': createTimeEle.val()};
                        }
                    },
                    {eventType:'change', eventSource:projectEle, data: function () {
                            return {'project': projectEle.select2("val")}
                        }
                    },
                    {eventType:'click', eventSource:$('button[name="search"]'), data: function () {
                            return {'search': searchEle.val()}
                        }
                    }
                ]
            });

            $("#approval").on('click', function () {
                var rows = $tableEle.bootstrapTable('getSelections');
                var data = rows[0];
                var count = rows.length;
                if(!count){
                    bees.tipWaring({
                        message: '请选择需要确认批准的工单'
                    });
                    return
                }

                bees.confirmBox({
                    title: '批准确认',
                    content: '您确定要<b class="text-success">批准</b>选中的工单吗？',
                    confirmCallback: function () {
                        setAjaxCSRFToken();
                        $.ajax({
                            type: 'post',
                            url: '{{ url_for("codes.audit-approval", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                            data : {},
                            success: function (result) {
                                if (result.code != 200) {
                                    bees.tipError({
                                        message: result.data.message
                                    });
                                }else {
                                   bees.tipSuccess({
                                        message: "工单已批准"
                                    });
                                }

                                //reload
                                table.bt.bootstrapTable('refresh');
                            }
                        })
                    }
                })
            });

            $("#refuse").on('click', function () {
                var rows = $tableEle.bootstrapTable('getSelections');
                var data = rows[0];
                var count = rows.length;
                if(!count){
                    bees.tipWaring({
                        message: '请选择需要拒绝批准的工单'
                    });
                    return
                }

                bees.confirmBox({
                    title: '批准确认',
                    content: '您确定要<b class="text-danger">拒绝</b>选中的工单吗？',
                    confirmCallback: function () {
                        setAjaxCSRFToken();
                        $.ajax({
                            type: 'post',
                            url: '{{ url_for("codes.audit-refuse", pk=default_pk) }}'.replace(bees.defaultPK, data.id),
                            data : {},
                            success: function (result) {
                                if (result.code != 200) {
                                    bees.tipError({
                                        message: result.data.message
                                    });
                                }else {
                                   bees.tipSuccess({
                                        message: "工单已拒绝"
                                    });
                                }

                                //reload
                                table.bt.bootstrapTable('refresh');
                            }
                        })
                    }
                })
            })

        })
    </script>
{% endblock %}
